//require CodeRAD
package com.codename1.samples;



import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.rad.controllers.Controller;
import com.codename1.rad.controllers.ControllerEvent;
import com.codename1.rad.controllers.ViewController;
import com.codename1.ui.Toolbar;
import com.codename1.rad.models.Entity;
import com.codename1.rad.models.EntityType;
import com.codename1.rad.models.StringProperty;
import com.codename1.rad.nodes.ActionNode;
import com.codename1.rad.nodes.ViewNode;
import com.codename1.rad.schemas.Thing;
import static com.codename1.rad.ui.UI.*;
import com.codename1.rad.ui.entityviews.ProfileAvatarView;
import com.codename1.ui.FontImage;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.FlowLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class ProfileAvatarViewSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        
        
        Form hi = new Form("Hi World", BoxLayout.y());
        
        Profile profile = new Profile();
        profile.set(Profile.name, "Steve");
        profile.set(Profile.icon, "https://www.codenameone.com/img/steve.jpg");
        
        ProfileAvatarView avatar = new ProfileAvatarView(profile, 30f);
        
        hi.add("Avatar with Name and Icon");
        hi.add(FlowLayout.encloseCenter(avatar));
        
        profile = new Profile();
        profile.set(Profile.name, "Steve");
        
        avatar = new ProfileAvatarView(profile, 30f);
        hi.add("Avatar with only Name");
        hi.add(FlowLayout.encloseCenter(avatar));
        
        profile = new Profile();
        avatar = new ProfileAvatarView(profile, 30f);
        hi.add("Avatar with no name or icon");
        hi.add(FlowLayout.encloseCenter(avatar));
        
        profile = new Profile();
        profile.set(Profile.name, "Steve");
        profile.set(Profile.icon, "https://www.codenameone.com/img/steve.jpg");
        hi.add("Avatar with view controller");
        hi.add(FlowLayout.encloseCenter(new ProfileViewController(null, profile).getView()));
        
        hi.show();
        
        
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }
    
    public static class Profile extends Entity {
        public static StringProperty name, icon;
        public static final EntityType TYPE = new EntityType() {{
            name = string(tags(Thing.name));
            icon = string(tags(Thing.thumbnailUrl));
            
        }};
    }
    
    
    public static class ProfileViewController extends ViewController {
        private static final ActionNode phone = action(
                icon(FontImage.MATERIAL_PHONE),
                label("Phone")
        );
                
        
        public ProfileViewController(Controller parent, Profile profile) {
            super(parent);
            ViewNode viewNode = new ViewNode(
                    actions(ProfileAvatarView.PROFILE_AVATAR_CLICKED_MENU, phone)
            );
            ProfileAvatarView avatar = new ProfileAvatarView(profile, viewNode, 20f);
            setView(avatar);
            
            addActionListener(phone, evt->{
                evt.consume();
                Dialog.show("Phone Action clicked", "For user "+evt.getEntity().getText(Thing.name), "OK", null);
            });
        }

        @Override
        public void actionPerformed(ControllerEvent evt) {
            System.out.println("Event "+evt);
        }
        
        
    }

}
